/**
 * Written By: MinHo Kim (minho.kim.korea@gmail.com)
 * Copyright (C) 2011, MinHo Kim, Software Expert Program, KAIST University, Seoul, Korea.
 * This program is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published 
 * by the Free Software Foundation; version 2 of the License.
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 * See the GNU General Public License for more details.
 */
package kaist.sep502.tsp.model;

import kaist.sep502.tsp.util.StopWatch;
import kaist.sep502.tsp.util.TspUtil;

/**
 * @author minho kim
 *
 */
public class Solution {
	public CityPath goalCityPath;

	private StopWatch stopWatch;
	public long expandedNodeCount = 0;
	private boolean isSuccess;
	public String algorithmName = "";
	
	public Solution(String algorithmName){
		expandedNodeCount = 0;
		this.algorithmName = algorithmName;
	}
	
	/**
	 * @param goalCityPath the goalCityPath to set
	 */
	public void setGoalCityPath(CityPath goalCityPath) {
		this.goalCityPath = goalCityPath;
	}
	
	public void startStopWatch(){
		stopWatch = new StopWatch();
	}
	
	public void stopStopWatch(){
		stopWatch.stop();
	}
	
	public String getElanpsedTime(){
		return stopWatch.toString();
	}
	
	/**
	 * @return
	 */
	public CharSequence successMessage() {
		String newLine = "\r\n";
		StringBuilder sb = new StringBuilder();
		
		sb.append("Algorithm : ").append(algorithmName).append(newLine);
		sb.append("Elapsed Time : ").append(stopWatch).append(newLine);
		sb.append("Expanded Nodes : ").append(this.expandedNodeCount).append(newLine);
		if(goalCityPath != null){
			sb.append("Path : ").append(goalCityPath.getPathListString()).append(newLine);
			sb.append("Path Cost : ").append(TspUtil.formatDouble(goalCityPath.getPathCost()));
		}
		
		return sb;
	}

	/**
	 * @return the isSuccess
	 */
	public boolean isSuccess() {
		return isSuccess;
	}

	/**
	 * @param isSuccess the isSuccess to set
	 */
	public void setSuccess(boolean isSuccess) {
		this.isSuccess = isSuccess;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		if(goalCityPath!= null && goalCityPath.tourHistory != null){
			return "[" + goalCityPath.getPathListString() + ":" + goalCityPath.getPathCost() + "]";
		}
		
		return "yet goalCityPath";
	}
}
